
const expess = require('express')

const router = expess.Router()


const utility = require('utility')
const  mysql =  require('../database/index')


router.use((req,resp,next)=>{
    // 写道他app.use(express.urlencoded({ extended: false }));之后
   // 才能获取 req.body
   // 结构并校验
   let { username, password } = req.body; 
   // 正则校验
   if (!/^\S{1,10}$/.test(username)) {
    //    return res.send({status: 1, message: '用户名不符合规则，1~10位非空白字符！'});
       return next(new Error('用户名1~10位非空白字符！'));
   }
   if (!/^\S{1,10}$/.test(password)) {
       // return res.send({status: 1, message: '密码不符合规则，1~10位非空白字符！'});
       return next(new Error('密码1~10位非空白字符！'));
   }
   // 如果通过了这两个校验，那么就说明用户名密码否符合规则，可以让路由处理了
   next();
   
})




router.post('/reguser',(req,resp,next)=>{
    // console.log(req.body);
    let {username,password}= req.body
  
   password=  utility.md5(password)
//    console.log(encode);
  
   mysql(`select * from user where username = "${username}"`,(err,res)=>{
        if(err) return resp.send({status:1,message: err.message})
        if(res.lenght>=1) return resp.send({status:1,message:'用于已存在'})  
        // resp.send({
        //     status:0,
        //     message:'成功'

        // })
        //用户不存在,插入数据
        let sql = `insert into user set username="${username}",password ="${password}";`;
        mysql(sql,(err,res)=>{
            if(err) return resp.send({status:11,message:err.message})
            // if(err) return next(err)
            if(res.affectedRows!=1) return resp.send({status:1,message:'注册失败'})
            resp.send({
                status:0,
                message:'注册成功'
            })
        })
   })
    console.log(username,password);
    // resp.send('注册成功')
})

router.post('/login',(req,resp)=>{
    let {username,password}= req.body
    password = utility.md5(password)
    let sql = `select * from user where username="${username}" and password="${password}";`
    mysql(sql,(err,res)=>{
        if(err) return resp.send({status:22,message: err.message})
        // console.log(res.length);
        console.log(req);
        // 具体的信息
        // console.log(res[0]);       
        if(res.length===0) return  resp.send({status:1,message:'没查询到该记录'})
        const jwt = require('jsonwebtoken')
        let userinfo = res[0]
        console.log(userinfo);
        delete res[0].nickname
        delete res[0].email
        delete res[0].user_pic
        console.log(userinfo);
        userinfo = {...userinfo}
        console.log(userinfo);
        // let secret = res[0].id
        const token = 'Bearer '+jwt.sign(userinfo,'yatmens',{expiresIn:'24h'})
        console.log(token);
        resp.send({
            "status": 0,
            "message": "登录成功！",
            // "token": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInBhc3N3b3JkIjoiIiwibmlja25hbWUiOiLms6Xlt7Tlt7QiLCJlbWFpbCI6Im5pYmFiYUBpdGNhc3QuY24iLCJ1c2VyX3BpYyI6IiIsImlhdCI6MTU3ODAzNjY4MiwiZXhwIjoxNTc4MDcyNjgyfQ.Mwq7GqCxJPK-EA8LNrtMG04llKdZ33S9KBL3XeuBxuI"
            "token":token
          })
    })
})


module.exports=router